<?php
namespace app\sub\controller;

use think\Db;

class Staff extends \Common {

    public function GetRoles(){
        $arr = [
            [
                "id" => 1,
                "name" => "入库员",
                "alias" => "editor"
            ],
            [
                "id" => 2,
                "name" => "出库员",
                "alias" => "outer"
            ],
            [
                "id" => 3,
                "name" => "会计员",
                "alias" => "cashier"
            ],
            [
                "id" => 4,
                "name" => "审核员",
                "alias" => "cashier"
            ],
        ];
        Code(200, false, $arr);
    }

    public function GetStaffs(){
        $aid = $this->token["id"];
        $admin = Db::name("admin")->where("pid", $aid)->select();
        $arr = [];
        if(!empty($admin)){
            foreach ($admin as $value){
                $arr[] = [
                    "id" => (int)$value["aid"],
                    "name" => $value["name"],
                    "password" => base64_decode($value["password64"]),
                    "role" => (int)$value["roles"],
                    "username" => $value["username"]
                ];
            }
        }
        Code(200, false, $arr);
    }

    public function UpdateStaff(){
        $aid = $this->token["id"];
        if(isset($_POST["name"]) && isset($_POST["role"]) && isset($_POST["username"]) && isset($_POST["password"])){
            $name = $_POST["name"]; $role = $_POST["role"]; $username = $_POST["username"]; $password = $_POST["password"];
            $info = [
                "name" =>  $name,
                "roles" => $role,
                "username" => $username,
                "password" => md5($password),
                "password64" => base64_encode($password),
                "role" => 0,
                "pid" => $aid
            ];
            if(isset($_POST["id"])){
                $id = $_POST["id"];
                $check = Db::name("admin")->where("aid != $id")->where("(role = 2 or role = 0)")->where("username", $username)->find();
                if(empty($check)){
                    Db::name("admin")->where("aid", $id)->update($info);
                    $admin = Db::name("admin")->where("aid", $id)->find();
                    $array = [
                        "id" => (int)$admin["aid"],
                        "name" => $admin["name"],
                        "password" => base64_decode($admin["password64"]),
                        "role" => (int)$admin["roles"],
                        "username" => $admin["username"]
                    ];
                    Code(200, "账号修改成功", $array);
                }else{
                    Code(403, "账号已经存在");
                }
            }else{
                $check = Db::name("admin")->where("(role = 2 or role = 0)")->where("username", $_POST["username"])->find();
                if(empty($check)){
                    $insert_id = Db::name("admin")->insertGetId($info);
                    $admin = Db::name("admin")->where("aid", $insert_id)->find();
                    $array = [
                        "id" => (int)$admin["aid"],
                        "name" => $admin["name"],
                        "password" => base64_decode($admin["password64"]),
                        "role" => (int)$admin["roles"],
                        "username" => $admin["username"]
                    ];
                    Code(200, "账号添加成功", $array);
                }else{
                    Code(403, "账号已经存在");
                }
            }
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }

}
